package com.wsoft.form.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wsoft.core.exception.Asserts;
import com.wsoft.form.dto.WfFormDTO;
import com.wsoft.form.entity.WfFormEntity;
import com.wsoft.form.entity.WfFormTableEntity;
import com.wsoft.form.enums.FormPublishEnum;
import com.wsoft.form.enums.FormStatusEnum;
import com.wsoft.form.manager.*;
import com.wsoft.form.mapper.WfFormTableMapper;
import com.wsoft.form.query.form.*;
import com.wsoft.form.service.IWfFormService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 自定义表单-页面表 服务实现类
 * </p>
 *
 * @author Z001
 * @since 2024年08月29日 09:11:06
 */
@Service
public class WfFormServiceImpl implements IWfFormService {

    @Resource
    private WfFormManager formManager;
    @Resource
    private WfFormTableRelationManager relationManager;
    @Resource
    private WfFormTableManager tableManager;
    @Resource
    private WfFormTableMapper tableMapper;
    @Resource
    private WfFormColumnManager columnManager;
    @Resource
    private WfFormVirtualColumnManager virtualColumnManager;
    @Resource
    private WfFormColumnLinkManager linkManager;

    /**
     * 新增页面
     *
     * @param query
     */
    @Override
    public boolean insertForm(FormInsertQuery query) {
        //校验formCode
        validateFormCode(query);

        //保存form
        WfFormEntity formEntity = Convert.convert(WfFormEntity.class, query);
        formEntity.setPublished(FormPublishEnum.NOT_PUBLISH.getValue());
        formEntity.setStatus(FormStatusEnum.BASIC.getValue());
        return formManager.save(formEntity);
    }

    @Override
    public boolean updateForm(FormUpdateQuery query) {
        //校验formCode
        validateFormCode(query);
        WfFormEntity formEntity = Convert.convert(WfFormEntity.class, query);
        return formManager.updateById(formEntity);
    }

    @Override
    public PageInfo<WfFormEntity> listForm(FormListQuery query) {
        PageHelper.startPage(query.getPageNum(), query.getPageSize());
        WfFormDTO dto = Convert.convert(WfFormDTO.class, query);
        return PageInfo.of(formManager.listWfForm(dto));
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delForm(Long formId) {
        List<WfFormTableEntity> tableEntityList = tableManager.getByFormId(formId);
        if(CollUtil.isNotEmpty(tableEntityList)){
            Asserts.fail("[wfrom.del.form.error]");
        }else{
            //删除页面
            formManager.removeById(formId);
        }
        return true;
    }

    /**
     * 获取详情
     *
     * @param id
     * @return
     */
    @Override
    public WfFormEntity getFormInfo(Long id) {
        return formManager.getById(id);
    }

    @Override
    public boolean updatePublish(FormPublishQuery query) {
        WfFormEntity formEntity = Convert.convert(WfFormEntity.class, query);
        return formManager.updateById(formEntity);
    }


    //############################### 私有函数 ###############################


    /**
     * 校验formCode 是否存在
     */
    private <T extends FormBaseQuery> void validateFormCode(T query) {
        WfFormEntity entity = formManager.getByFormCode(query.getFormCode());
        if (query instanceof FormInsertQuery && null != entity) {
            //页面编码已存在
            Asserts.fail("[wform.pageCode.exist]");
        }
        if (query instanceof FormUpdateQuery && null != entity && !((FormUpdateQuery) query).getId().equals(entity.getId())) {
            //页面编码已存在
            Asserts.fail("[wform.pageCode.exist]");
        }
    }

}
